package com.linkage.service.auth;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.linkage.pojo.background.UserRole;
import com.linkage.pojo.consumer.User;
import com.linkage.service.background.UserManageService;
import com.linkage.service.consumer.UserService;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author ryan
 *
 */
@Service
public class AdminLoginService {
    @Autowired
    private UserService userService;

    @Autowired
    private UserManageService userManageService;


    public String adminLogin(HttpServletRequest request, String password, String loginName) {

         String errorMsg = "";

        if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) {
            errorMsg = "账户或者密码不能为空！";
            return errorMsg;
        }

        User adminUser = this.userService.getOneUserByLoginName(loginName);

        System.out.println("admin是否存在:"+adminUser);
        
        if (adminUser != null) {

           if( !(password.equals(adminUser.getPassword()))){
               errorMsg = "账户密码错误！";
               return errorMsg;
           }
            UserRole userRole = this.userManageService.findUserRoleById(adminUser.getUserId());
            System.out.println("userole是否存在:"+userRole);
            if (userRole != null) {
                String roleName = userRole.getRoleName();
                if ("商品管理员".equals(roleName) || "超级管理员".equals(roleName) || "订单管理员".equals(roleName)
                        || "用户管理员".equals(roleName)) {

                    storeInSession(request.getSession(), "ADMIN_USER_KEY_IN_SESSION", adminUser);
                    storeInSession(request.getSession(), "ADMIN_ROLE_NAME_IN_SESSION", roleName);
                    
                    return  errorMsg;

                }else if("封禁用户".equals(roleName)){
                    System.out.println("您的账户已经被封！");
                    errorMsg = "您的账户已经被封！";
                    return errorMsg;
                }
                else{
                    System.out.println("不是管理员 ");
                    errorMsg = "您的账户不是管理员！";
                    return errorMsg;
                }
            } else {
                System.out.println("您的账户没有权限！");
                errorMsg = "您的账户没有权限！";
                return errorMsg;
            }
        }
        errorMsg = "登录异常！";
        return errorMsg;

    }

    public boolean logout(HttpServletRequest request) {
        Object obj = request.getSession().getAttribute("ADMIN_USER_KEY_IN_SESSION");
        if (obj == null || !(obj instanceof User))
            return false;
        request.getSession().removeAttribute("ADMIN_ROLE_NAME_IN_SESSION");
        return true;
    }

    private void storeInSession(HttpSession session, String key, Object object) {
        session.setAttribute(key, object);
    }

}
